package com.atf.atfconfigurator.views;

import java.util.Iterator;

import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;


import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.composite.CollapseableBox;
import com.atf.atfconfigurator.utils.WasConstant;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class DataSourceView extends VerticalLayout implements View{
	private PropertiesConfiguration wasConfigProperty;
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();
		
		setSpacing(true);
        setMargin(true);
        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
        addComponent(new CollapseableBox("DB2 UDB for iseries DataSource",getDataSource("DB2iseriesDS"), false));
        addComponent(new CollapseableBox("DB2 Universal DataSource",getDataSource("DB2UniversalDS"), false));
        addComponent(new CollapseableBox("Oracle DataSource",getDataSource("OracleDS"), false));
        addComponent(new CollapseableBox("Microsoft SQL Server DataSource",getDataSource("MSSQLDS"), false));
        addComponent(new CollapseableBox("TerraData DataSource",getDataSource("TerraDataDS"), false));
	}
	private Component getDataSource(final String type) {
		// TODO Auto-generated method stub
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        if( wasConfigProperty.containsKey("numberof"+type) || wasConfigProperty.containsKey("numberOf"+type)){
        	final TextField nds = new TextField("Number of "+type+" DatSource");
        	int ndsValue=0;
        	if(wasConfigProperty.containsKey("numberof"+type)){
        		 ndsValue=wasConfigProperty.getInt("numberof"+type);
        	}
        	if(wasConfigProperty.containsKey("numberOf"+type)){
        		ndsValue=wasConfigProperty.getInt("numberOf"+type);
        	}
           
            nds.setValue(String.valueOf(ndsValue));
            nds.setWidth("50%");
            //nJDBC.setIcon(FontAwesome.PLUS);
            nds.addValueChangeListener(new Property.ValueChangeListener(){

    			/**
    			 * 
    			 */
    			private static final long serialVersionUID = 1L;

    			@Override
    			public void valueChange(ValueChangeEvent event) {
    				// TODO Auto-generated method stub
    				if(wasConfigProperty.containsKey("numberof"+type)){
    					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("numberof"+type,  (String) event.getProperty().getValue());
    	        	}
    	        	if(wasConfigProperty.containsKey("numberOf"+type)){
    	        		((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("numberOf"+type,  (String) event.getProperty().getValue());
    	        	}
    				
    				((AtfconfiguratorUI) getUI()).navigator.navigateTo("DataSource");
    			}        	
            });
            form.addComponent(nds);
            for(int i=1; i<ndsValue+1; i++){
            	form.addComponent(new CollapseableBox(String.valueOf(i),getDataSourceEntries(type, i), true));
            }
        }else{
        	form.addComponent(getDataSourceEntries(type, 0));
        }
		return form;
	}
	
	private Component getDataSourceEntries(final String type, final int pos) {
		// TODO Auto-generated method stub
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        
        HierarchicalConfiguration sub = WasConstant.getWasConstantFile().configurationAt("dataSources."+type);
        Iterator iterator=sub.getKeys();
        while(iterator.hasNext()){
        	String key=(String) iterator.next();
        	
        	String value=sub.getString(key);
        	
        	String posKey="";
        	if( pos >0){
        		posKey=type+pos+key;
        	}else{
        		posKey=type+key;
        	}
        	if( wasConfigProperty.containsKey(posKey)){
        		
        			value=wasConfigProperty.getString(posKey);
        	
        	}else{
        		wasConfigProperty.addProperty(posKey, value);
        	}
        	
        	if(key.equalsIgnoreCase("JDBC")){
        		NativeSelect jdbcField=getJDBCProviderSelectField(type);
        		jdbcField.setValue(value);
        		jdbcField.addValueChangeListener(new Property.ValueChangeListener(){

    				@Override
    				public void valueChange(ValueChangeEvent event) {
    					// TODO Auto-generated method stub
    					String value = (String) event.getProperty().getValue();
    					if(pos > 0){
    						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+pos+"JDBC", value);
    					}else{
    						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+"JDBC", value);
    					}
    					
    				}
                	
                });
        		jdbcField.setImmediate(true);
                form.addComponent(jdbcField);
        	}else if(key.equalsIgnoreCase("AliasName")){
        		
        		NativeSelect aliasNameField=getAliasNames();
        		aliasNameField.setValue(value);
        		aliasNameField.addValueChangeListener(new Property.ValueChangeListener(){

    				@Override
    				public void valueChange(ValueChangeEvent event) {
    					// TODO Auto-generated method stub
    					String value = (String) event.getProperty().getValue();
    					if(pos > 0){
    						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+pos+"AliasName", value);
    					}else{
    						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+"AliasName", value);
    					}
    					
    				}
                	
                });
        		aliasNameField.setImmediate(true);
                form.addComponent(aliasNameField);
        		
        	}else if(key.equalsIgnoreCase("CustomProperty")){
        		final TextArea customProperty = new TextArea("Custom Property");
        		customProperty.setWidth("100%");
        		customProperty.setValue(value);
        		customProperty.addValueChangeListener(new Property.ValueChangeListener(){

        			@Override
        			public void valueChange(ValueChangeEvent event) {
        				// TODO Auto-generated method stub
        				String value = (String) event.getProperty().getValue();
        				if(pos > 0){
    						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+pos+"CustomProperty", value);
    					}else{
    						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+"CustomProperty", value);
    					}
        			}
                	
                });
                form.addComponent(customProperty);
        	}else{
        		final TextField dsField = new TextField(key);
            	dsField.setWidth("100%");
            	dsField.setValue(value);
            	dsField.addValueChangeListener(new Property.ValueChangeListener(){

    				@Override
    				public void valueChange(ValueChangeEvent event) {
    					// TODO Auto-generated method stub
    					String value = (String) event.getProperty().getValue();
    					if(pos > 0){
    						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+pos+dsField.getCaption(), value);
    					}else{
    						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+dsField.getCaption(), value);
    					}
    					
    				}
                	
                });
            	dsField.setImmediate(true);
                form.addComponent(dsField);
        	}
        	
        }
        
        
		return form;
	}
	private NativeSelect getJDBCProviderSelectField(String type){
	     NativeSelect jdbcField = new NativeSelect("JDBC Provider");
	     jdbcField.setWidth("50%");
	     jdbcField.setNullSelectionAllowed(false);
	     if(type.endsWith("DS")){
	    	 //type=StringUtils.removeEnd(type, "DS");
	    	 type=type.substring(0,(type.length()-2)); 
	     }
	     type="JDBC"+type;
	     int value=wasConfigProperty.getInt("numberof"+type, 0);
	     for(int i=1; i<value+1; i++){
	    	 String name =wasConfigProperty.getString(type+i+"Name");
	    	 jdbcField.addItem(name);
         }
	     
	     return jdbcField;
	}
	
	private NativeSelect getAliasNames(){
		NativeSelect aliasNamesField = new NativeSelect("J2C Alias");
		aliasNamesField.setWidth("50%");
		aliasNamesField.setNullSelectionAllowed(false);
		int numberOfAlias=wasConfigProperty.getInt("numberOfAlias", 0);
		for(int i=1; i<numberOfAlias+1; i++){
	    	 String name =wasConfigProperty.getString("Alias"+i+"Name");
	    	 aliasNamesField.addItem(name);
        }
		
	    return aliasNamesField;
	    		 
	}
}
